package com.xiaohu.myspace.buiness.buinessinterfaceimple;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.xiaohu.myspace.buiness.BaseBuiness;
import com.xiaohu.myspace.buiness.buinessinterface.MessageInfoInterface;
import com.xiaohu.myspace.database.dao.MessageInfoDao;
import com.xiaohu.myspace.database.entity.MessageInfo;
import com.xiaohu.myspace.vo.MessageInfoVo;

public class MessageInfoInterfaceImple extends BaseBuiness implements
		MessageInfoInterface {

	private MessageInfoDao messageDao;

	public MessageInfoDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageInfoDao messageDao) {
		this.messageDao = messageDao;
	}

	@Override
	public List<MessageInfoVo> getMessageInfoWithHQL(int messageType,
			int pageNum) {

		final int start = (pageNum - 1) * pageSize;
		final String sql = "select m.message_title,m.message_context,m.message_date,m.message_up_down,m.message_comment_count,u.user_name,u.user_sex,u.user_age,u.user_photo,u.user_grade from message_info m left join user_info u on m.message_editor=u.id  where m.message_type="
				+ messageType;

		List<MessageInfoVo> data = doSomeThingInDB(new DoInBuiness<List<MessageInfoVo>>() {

			@Override
			public List<MessageInfoVo> doSomeThingInBuiness(
					SessionFactory sessionFactory, Session session) {
				return messageDao.getMessageInfoWithHQL(sql, start, pageSize,
						session);
			}

		});
		return data;
	}

	@Override
	public boolean addMessage(final MessageInfo messageInfo) {
		return doSomeThingInDB(new DoInBuiness<Boolean>() {

			@Override
			public Boolean doSomeThingInBuiness(SessionFactory sessionFactory,
					Session session) {
				return messageDao.addMessage(messageInfo, session);
			}

		});
	}

	@Override
	public boolean deleteMessage(final MessageInfo messageInfo) {
		return doSomeThingInDB(new DoInBuiness<Boolean>() {

			@Override
			public Boolean doSomeThingInBuiness(SessionFactory sessionFactory,
					Session session) {
				return messageDao.deleteMessage(messageInfo, session);
			}

		});
	}

	@Override
	public boolean updateMessage(final MessageInfo messageInfo) {
		return doSomeThingInDB(new DoInBuiness<Boolean>() {

			@Override
			public Boolean doSomeThingInBuiness(SessionFactory sessionFactory,
					Session session) {
				return messageDao.updateMessage(messageInfo, session);
			}

		});
	}

	@Override
	public MessageInfo findMessageWithMessageId(final int messageId) {
		return doSomeThingInDB(new DoInBuiness<MessageInfo>() {

			@Override
			public MessageInfo doSomeThingInBuiness(
					SessionFactory sessionFactory, Session session) {
				return messageDao.findMessageWithMessageId(messageId, session);
			}

		});
	}

}
